package com.cris.controller;

/**
 * Created by owen on 2017/5/18.
 */

import com.cris.service.ChartService;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;


@Controller
public class ChartsController {
    @Autowired
    ChartService chartService;
    //进入报表界面
    @RequestMapping(value = "/chart",method = RequestMethod.GET)
    public String chart(){
        return "charts";
    }
    //获取当年按月度统计的报表
    @RequestMapping(value = "/chart_month", method = RequestMethod.GET)
    public void getMonthChart(HttpServletRequest request,
                             HttpServletResponse response) {
        response.setContentType("image/png");
        String currentYear = Calendar.getInstance().getWeekYear()+"";
        CategoryDataset cySet = createDataSet(currentYear);

        String titel = Calendar.getInstance().getWeekYear()+"";
        JFreeChart chart = createChart(cySet, titel+" Sales Chart","Month");
        try {
            ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
                    750, 400);
            response.getOutputStream().close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private CategoryDataset createDataSet(String year) {

        DefaultCategoryDataset dcd = new DefaultCategoryDataset();
        dcd.addValue(chartService.getJanAmount(year),"1","1");
        dcd.addValue(chartService.getFebAmount(year),"2","2");
        dcd.addValue(chartService.getMarchAmount(year),"3","3");
        dcd.addValue(chartService.getAprilAmount(year),"4","4");
        dcd.addValue(chartService.getMayAmount(year),"5","5");
        dcd.addValue(chartService.getJuneAmount(year),"6","6");
        dcd.addValue(chartService.getJulyAmount(year),"7","7");
        dcd.addValue(chartService.getAugAmount(year),"8","8");
        dcd.addValue(chartService.getSeptAmount(year),"9","9");
        dcd.addValue(chartService.getOctAmount(year),"10","10");
        dcd.addValue(chartService.getNovAmount(year),"11","11");
        dcd.addValue(chartService.getDecAmount(year),"12","12");
        return dcd;
    }
    private JFreeChart createChart(CategoryDataset cySet, String chartTitle,String axis) {
        JFreeChart chart = ChartFactory.createBarChart3D(chartTitle,axis,"SalesAmount",cySet, PlotOrientation.VERTICAL
        ,true,true,true);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        return chart;
    }

    //选择起止时间，按产品类别进行分类统计销售额，生成图形报表

    @RequestMapping(value = "/chart_type",method = RequestMethod.POST)
    public void getTypeChart(HttpServletRequest request,
                             HttpServletResponse response) throws ParseException {
        response.setContentType("image/png");
        String from = request.getParameter("from");
        String to = request.getParameter("to");
        Date date1 = new SimpleDateFormat("MM/dd/yyyy").parse(from);
        Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse(to);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        from = sdf.format(date1);
        to = sdf.format(date2);
            CategoryDataset cySet = createTypeDataSet(from,to);
            String titel = "from "+from+" to "+to;


        JFreeChart chart = createChart(cySet, titel+" Sales Chart","ProductType");
        try {
            ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
                    750, 400);
            response.getOutputStream().close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    private CategoryDataset createTypeDataSet(String from,String to) {

        DefaultCategoryDataset dcd = new DefaultCategoryDataset();
        //需要金额和类型
        Map<String,Double> map =  chartService.getAmountBetween(from,to);
        System.out.println(map);
        for (Map.Entry<String,Double> entry:map.entrySet()){
            dcd.addValue(entry.getValue(),entry.getKey(),entry.getKey());
        }
        return dcd;
    }


}